﻿//**************************************************
//Description:  编辑器管理
//Author:       leubooth
//Date:         2016.11.04
//**************************************************

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Pathfinding.Serialization.JsonFx;
using System;

public class EditorManager : MonoBehaviour
{
    /// <summary>
    /// 单件
    /// </summary>
    public static EditorManager Instance;

    /// <summary>
    /// 编辑器状态机
    /// </summary>
    public EditorState state = EditorState.None;

    /// <summary>
    /// 关卡数据基础类实例
    /// </summary>
    public LevelBase levelBase;

    /// <summary>
    /// 路径点Object
    /// </summary>
    List<GameObject> objList = new List<GameObject>();

    /// <summary>
    /// 关卡配置名称
    /// </summary>
    public string levelName = "0";

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        levelBase = new LevelBase();

        GridSystem.Instance.InitGridSystem();
    }

    void Update()
    {
        switch (state)
        {
            case EditorState.None:
                break;
            case EditorState.SetDirection:
                break;
            case EditorState.SetBorn:
                SetBorn();
                break;
            case EditorState.SetPath:
                SetPath();
                break;
        }
    }

    void SetPath()
    {
        //添加路径节点
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.GetComponent<Grid>())
                {
                    if (!objList.Contains(hit.transform.gameObject))
                    {
                        hit.transform.GetChild(0).GetComponent<TextMesh>().text = objList.Count.ToString();
                        objList.Add(hit.transform.gameObject);
                    }
                }
            }
        }
        //退回路径节点
        if (Input.GetMouseButtonDown(1))
        {
            if (objList.Count == 0) return;
            objList[objList.Count - 1].transform.GetChild(0).GetComponent<TextMesh>().text = "";
            objList.RemoveAt(objList.Count - 1);
        }
    }

    public GameObject lastObject = null;
    public Color lastObjectColor = Color.white;

    void SetBorn()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.GetComponent<Grid>())
                {
                    if (lastObject != null)
                    {
                        lastObject.GetComponent<MeshRenderer>().material.color = lastObjectColor;
                    }

                    lastObject = hit.transform.gameObject;
                    lastObjectColor = hit.transform.GetComponent<MeshRenderer>().material.color;

                    hit.transform.GetComponent<MeshRenderer>().material.color = Color.green;

                    Grid grid = hit.transform.GetComponent<Grid>();
                    levelBase.born = new Vector3(grid.x, grid.y);
                }
            }
            //else
            //{
            //    if (lastObject != null)
            //    {
            //        lastObject.GetComponent<MeshRenderer>().material.color = lastObjectColor;
            //    }

            //    lastObject = null;
            //}
        }
    }

    void OnGUI()
    {
        if (GUILayout.Button("设定元素运动方向"))
        {
            state = EditorState.SetDirection;
        }
        if (GUILayout.Button("设定元素生成点"))
        {
            state = EditorState.SetBorn;
        }
        if (GUILayout.Button("设定元素轨迹路径"))
        {
            state = EditorState.SetPath;
        }
        levelName = GUILayout.TextField(levelName);
        if (GUILayout.Button("保存关卡配置"))
        {
            levelBase.path.Clear();
            for (int i = 0; i < objList.Count; i++)
            {
                Vector3 vector = new Vector3(objList[i].GetComponent<Grid>().x, objList[i].GetComponent<Grid>().y,0.0f);
                levelBase.path.Add(vector);
            }

            LevelDataManager.WriteLevelData(levelBase,levelName);
        }

        switch (state)
        {
            case EditorState.SetDirection:
                if (GUILayout.Button("下落"))
                {
                    levelBase.direction = 0;
                    state = EditorState.None;
                }
                if (GUILayout.Button("螺旋"))
                {
                    levelBase.direction = 1;
                    state = EditorState.None;
                }
                if (GUILayout.Button("S形"))
                {
                    levelBase.direction = 2;
                    state = EditorState.None;
                }
                break;
        }
    }
}

/// <summary>
/// 编辑器状态
/// </summary>
public enum EditorState
{
    None,
    SetDirection,
    SetBorn,
    SetPath
}